﻿using System.Windows.Media;
using C1.Silverlight.Pdf;
using System.Windows;
using System.Collections.Generic;

namespace MyFloridaHouse.libSvOpenDistricts.PDF
{
    public static class HouseMapCompass
    {
        public static void Draw(C1PdfDocument pdf, PDFPoint compassOrigin, double compassWidth)
        {
            PDFPoint compassCenter = new PDFPoint(compassOrigin.x + compassWidth / 2, compassOrigin.y + compassWidth / 2);

            double compassWidthHalf = compassWidth / 2;
            double starUnit = compassWidth * .125;
            double starUnitHalf = starUnit / 2;

            double starLong = compassWidthHalf - starUnit;
            double starMid = starLong * .5;
            double starInnerShort = starLong * .15;
            double starInnerMedium = starLong * .3;

            double textHeight = starUnit;
            double textWidth = textHeight;

            double heightAdjust = 1.5; // pdf print seems to shift letters down out of print box, only affects appearance of "south"; number found by experimentation

            PDFString north = new PDFString(pdf, "Arial", textHeight, PdfFontStyle.Bold, Colors.Black, "N", 1.0);
            PDFString south = new PDFString(pdf, "Arial", textHeight, PdfFontStyle.Bold, Colors.Black, "S", 1.0);
            PDFString east = new PDFString(pdf, "Arial", textHeight, PdfFontStyle.Bold, Colors.Black, "E", 1.0);
            PDFString west = new PDFString(pdf, "Arial", textHeight, PdfFontStyle.Bold, Colors.Black, "W", 1.0);
            
            DrawLetter(pdf, north, new PDFPoint(compassOrigin.x + compassWidthHalf - north.textBoxSize.Width / 2, compassOrigin.y));
            DrawLetter(pdf, south, new PDFPoint(compassOrigin.x + compassWidthHalf - south.textBoxSize.Width / 2, compassOrigin.y + compassWidth - south.textBoxSize.Height * heightAdjust));
            DrawLetter(pdf, east, new PDFPoint(compassOrigin.x + compassWidth - west.textBoxSize.Width, compassOrigin.y + compassWidthHalf - east.textBoxSize.Height/2));
            DrawLetter(pdf, west,  new PDFPoint(compassOrigin.x , compassOrigin.y + compassWidthHalf - west.textBoxSize.Height/2));


            // Draw Compass star
            double penSize = 1.0;
            DrawCompassStarPoint(pdf, compassCenter, -starLong, 0.0, -starInnerMedium, -starInnerShort, true, penSize);
            DrawCompassStarPoint(pdf, compassCenter, -starMid, -starMid, -starInnerMedium, -starInnerShort, false, penSize);
            DrawCompassStarPoint(pdf, compassCenter, -starMid, -starMid, -starInnerShort, -starInnerMedium, true, penSize);
            DrawCompassStarPoint(pdf, compassCenter, 0.0, -starLong, -starInnerShort, -starInnerMedium, false, penSize);

            DrawCompassStarPoint(pdf, compassCenter, 0.0, -starLong, starInnerShort, -starInnerMedium, true, penSize);
            DrawCompassStarPoint(pdf, compassCenter, starMid, -starMid, starInnerShort, -starInnerMedium, false, penSize);
            DrawCompassStarPoint(pdf, compassCenter, starMid, -starMid, starInnerMedium, -starInnerShort, true, penSize);
            DrawCompassStarPoint(pdf, compassCenter, starLong, 0.0, starInnerMedium, -starInnerShort, false, penSize);

            DrawCompassStarPoint(pdf, compassCenter, starLong, 0.0, starInnerMedium, starInnerShort, true, penSize);
            DrawCompassStarPoint(pdf, compassCenter, starMid, starMid, starInnerMedium, starInnerShort, false, penSize);
            DrawCompassStarPoint(pdf, compassCenter, starMid, starMid, starInnerShort, starInnerMedium, true, penSize);
            DrawCompassStarPoint(pdf, compassCenter, 0.0, starLong, starInnerShort, starInnerMedium, false, penSize);

            DrawCompassStarPoint(pdf, compassCenter, 0.0, starLong, -starInnerShort, starInnerMedium, true, penSize);
            DrawCompassStarPoint(pdf, compassCenter, -starMid, starMid, -starInnerShort, starInnerMedium, false, penSize);
            DrawCompassStarPoint(pdf, compassCenter, -starMid, starMid, -starInnerMedium, starInnerShort, true, penSize);
            DrawCompassStarPoint(pdf, compassCenter, -starLong, 0.0, -starInnerMedium, starInnerShort, false, penSize);

        }

        private static void DrawCompassStarPoint(C1PdfDocument pdf, PDFPoint compassCenter, double offsetOuterX, double offsetOuterY, double offsetInnerX, double offsetInnerY, bool fill, double penSize)
        {
            List<Point> pointList = new List<Point>();

            pointList.Add(new Point(compassCenter.x, compassCenter.y));  // start drawing star point at compass center
            pointList.Add(new Point(compassCenter.x + offsetOuterX, compassCenter.y + offsetOuterY));
            pointList.Add(new Point(compassCenter.x + offsetInnerX, compassCenter.y + offsetInnerY));
            pointList.Add(new Point(compassCenter.x, compassCenter.y));  // end drawing star point at compass center
            Point[] XYPoints = pointList.ToArray();
            if (fill)
            {
                pdf.FillPolygon(Colors.Black, XYPoints);
            }
            else
            {
                pdf.DrawPolygon(new Pen(Colors.Black, penSize), XYPoints);
            }
        }

        private static void DrawLetter(C1PdfDocument pdf, PDFString direction, PDFPoint origin)
        {

            pdf.DrawString(direction.text, direction.font, Colors.Black, new Rect(origin.x, origin.y, direction.textBoxSize.Width, direction.textBoxSize.Height));
        }

    }
}

